package com.wlstock.funi.Trader;

import android.content.Context;

import com.wlstock.funi.dao.PositionDao;

import java.util.Date;
import java.util.List;

import static com.wlstock.funi.fundenum.TRADE_ERROR_STATE.ERROR_NOT_ANY_STOCK;
import static com.wlstock.funi.fundenum.TRADE_ERROR_STATE.ERROR_NOT_AUTO;
import static com.wlstock.funi.fundenum.TRADE_ERROR_STATE.ERROR_NOT_ENOUGH_MONEY;
import static com.wlstock.funi.fundenum.TRADE_ERROR_STATE.ERROR_NOT_INTIME;
import static com.wlstock.funi.fundenum.TRADE_ERROR_STATE.ERROR_NOT_NEW_POSITION;

/**
 * Created by admin on 16/9/18.
 */

public abstract class BaseTrader {

    private TraderNameEnum name;
    public int tradeType;
    private Context mContext;

    protected BaseTrader(Context mContext,int tradeType,TraderNameEnum enumName) {
        this.mContext = mContext;
        this.name  = enumName;
        this.tradeType = tradeType;
    }

    protected abstract void trade();

    public void  trade(TraderListener tListener,TradeResultListener tRListener){
        if (tListener!=null){
            TraderConfiger configer = TraderConfigerManger.getTraderConfiger(name,mContext);
            trade(tListener,configer,tRListener);
        }
    }

    private void trade(TraderListener tListener, TraderConfiger configer,TradeResultListener tRListener) {
        if (configer!=null && tRListener!=null){
            boolean inTradeTime = tListener.isInTradeTime(configer.getTradeTimeParams());
            if (inTradeTime){
                if (PositionDao.getInstance(mContext).isNewlyPosition(new Date())) {
                    boolean isTragger = tListener.tradeTrigger(configer.getTradderTriggerParams());
                    if (isTragger){
                        List<TraderObject> traderObjects = tListener.tradeCode(configer.getStkCodeParams());
                        if (traderObjects!=null && !traderObjects.isEmpty()){
                            long totalMoney = tListener.tradeTotalMoney(configer.getTotalMoneyParams());
                            if (totalMoney > 0){
                                if (configer.getIsAutoTrade()){
                                    tRListener.tradeSuccess(this,tradeType,traderObjects, totalMoney);
                                }else {
                                    tRListener.tradeError(this,tradeType, TraderError.getError(ERROR_NOT_AUTO));
                                }
                            }else {
                                tRListener.tradeError(this,tradeType, TraderError.getError(ERROR_NOT_ENOUGH_MONEY));
                            }
                        }else {
                            tRListener.tradeError(this,tradeType, TraderError.getError(ERROR_NOT_ANY_STOCK));
                        }
                    }else {
                        tRListener.tradeError(this,tradeType, tListener.generateTriggerErrorState());
                    }
                }else {
                    tRListener.tradeError(this,tradeType, TraderError.getError(ERROR_NOT_NEW_POSITION));
                }
            }else {
                tRListener.tradeError(this,tradeType, TraderError.getError(ERROR_NOT_INTIME));
            }
        }
    }


}
